package com.questionnaires.controller;

import com.questionnaires.model.Response;
import com.questionnaires.model.User;
import com.questionnaires.service.ResponseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 * @author ioana.ruca@gmail.com
 */
@Controller
public class ResponseController {

    public static final String RESPONSE_LIST_SESSION_ATTR = "responseList";

    @Autowired
    ResponseService responseService;

    @RequestMapping(value = "/addResponse", method = RequestMethod.POST)
    public String saveResponse(@ModelAttribute("response")
                               @Valid Response response, BindingResult result,
                               HttpSession httpSession) {
        User user = (User)httpSession.getAttribute("user");
        if (user == null || !user.checkUserManager() )
            return UserController.REDIRECT_INDEX;
        if(result.hasErrors()) {
            return null;
        }
        List<Response> responseList = new ArrayList<Response>();
        responseList.add(response);
        httpSession.setAttribute(RESPONSE_LIST_SESSION_ATTR, responseList);

        return null;
    }
}
